-- thrown knife

if SERVER then
   AddCSLuaFile("shared.lua")
else
   ENT.PrintName = "giblet"
end

game.AddDecal( "Cross", "decals/cross" );

ENT.Type = "anim"
ENT.GibModel = Model("models/props_junk/Rock001a.mdl")
ENT.MinVelocity = -400
ENT.MaxVelocity = 400
ENT.Bounces = 0
ENT.MaxBounces = 1
ENT.DestroyTime = 5
ENT.SplatReach = 8

ENT.ImpactSounds = {
	Sound("physics/flesh/flesh_squishy_impact_hard2.wav"),
}

ENT.BreakSounds = {
	Sound("physics/body/body_medium_break2.wav"),
	Sound("physics/body/body_medium_break3.wav"),
	Sound("physics/body/body_medium_break4.wav")
}

ENT.GibModels = {
	Model("models/quakegibs/quakemdl/abdomen.mdl"),
	Model("models/quakegibs/quakemdl/arm.mdl"),
	Model("models/quakegibs/quakemdl/brain.mdl"),
	Model("models/quakegibs/quakemdl/chest.mdl"),
	Model("models/quakegibs/quakemdl/fist.mdl"),
	Model("models/quakegibs/quakemdl/foot.mdl"),
	Model("models/quakegibs/quakemdl/forearm.mdl"),
	Model("models/quakegibs/quakemdl/intestine.mdl"),
	Model("models/quakegibs/quakemdl/leg.mdl"),
	Model("models/quakegibs/quakemdl/skull.mdl"),
}

if SERVER then

	function ENT:Initialize()
		self.Entity:PhysicsInitSphere( 3, "metal_bouncy" )
		self:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
		self.Entity:SetSolid(SOLID_VPHYSICS)
		self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
		--self.Entity:SetMaterial("models/shadertest/shader4.vmt")
		self.Entity:SetGravity(4);
		
		local phys = self:GetPhysicsObject()
		if ( IsValid( phys ) ) then
			phys:Wake()
			phys:SetVelocity(Vector(math.random(self.MinVelocity, self.MaxVelocity), 
			math.random(self.MinVelocity, self.MaxVelocity),
			math.random(self.MinVelocity, self.MaxVelocity)))
		end
	end
	
	function ENT:SetMode(mode)
		if(mode == "big")then
			self:Big()
		elseif(mode == "bigsplit")then
			self:BigSplit()
		end
	end
	
	function ENT:Big()
		ParticleEffectAttach( "blood_hands", PATTACH_ABSORIGIN_FOLLOW, self, 0)
		self.PhysicsCollide = function( data )
			self:EmitSound( self.ImpactSounds[math.random(1, #self.ImpactSounds)], 75, 100 + math.random(-35, 10) )
			self.Bounces = self.Bounces + 1
			self:PaintBlood()
			if(self.Bounces >= self.MaxBounces)then
				local phys = self:GetPhysicsObject()
				if ( IsValid( phys ) ) then
					self:StopParticles()
					phys:Sleep()
					ParticleEffect( "blood_erupt", self:GetPos(), self:GetAngles(), self )
					timer.Simple(self.DestroyTime, function() if(IsValid(self))then self:Remove() end end)
				end
			elseif(self.Bounces < self.MaxBounces)then
				self:EmitSound( self.ImpactSounds[math.random(1, #self.ImpactSounds)], 75, 100 + math.random(-35, 10) )
				ParticleEffect( "blood_erupt", self:GetPos(), self:GetAngles(), self )
				self:PaintBlood()
			end
		end
	end
	
	function ENT:BigSplit()
		ParticleEffectAttach( "blood_hands", PATTACH_ABSORIGIN_FOLLOW, self, 0)
		self.PhysicsCollide = function( data )
			self.Bounces = self.Bounces + 1
			self:PaintBlood()
			if(self.Bounces >= self.MaxBounces)then
				self:EmitSound( self.BreakSounds[math.random(1, #self.BreakSounds)], 75, 100 + math.random(-35, 10) )
				local phys = self:GetPhysicsObject()
				if ( IsValid( phys ) ) then
					self:StopParticles()
					phys:Sleep()
					ParticleEffect( "blood_erupt", self:GetPos(), self:GetAngles(), self )
					for i = 1, 2, 1 do
						local gib = ents.Create("gore_giblet")
						gib:Initialize();
						gib:SetMode("big")
						gib:SetModel(self:GetModel())
						gib:SetModelScale(0.5, 0)
						gib:SetPos(self:GetPos() + Vector(0, 0, 8))
						gib.MaxBounces = 1
					end
					timer.Simple(self.DestroyTime, function() if(IsValid(self))then self:Remove() end end)
				end
			elseif(self.Bounces < self.MaxBounces)then
				self:EmitSound( self.ImpactSounds[math.random(1, #self.ImpactSounds)], 75, 100 + math.random(-35, 10) )
				ParticleEffect( "blood_erupt", self:GetPos(), self:GetAngles(), self )
				self:PaintBlood()
			end
		end
	end
	
	function ENT:PaintBlood()
		local tr = util.TraceLine({start=self:GetPos(), endpos=(self:GetPos() + Vector(0,0,-256)), filter=ignore, mask=MASK_SOLID})
		util.Decal("Blood", tr.HitPos+tr.HitNormal, tr.HitPos-tr.HitNormal)
	end
end


